var express = require('express');
var router = express.Router();
var Comments = require('../db/commentsO');
var keep = require('../db/keep');
var fans = require('../db/fans');
var VandB = require('../db/VandB');

/* GET users listing. */

router.get('/', function(req, res, next){
	VandB.find(req, 'video', function(d){
		if (d.status == 200) {
			res.status(200).render('video/videoList',{
				'title':'videoList',
				'user':req.infos,
				'video':d.dt
			})
		} else {
			res.status(400).render('404',{})
		}
	})
})

router.get('/av/:id', function(req, res, next) {
	try{
		req.VandB.findById(req.params.id, function(err, result1){
			if (err) {
				res.status(400).render('404',{'err':err});
			} else {
				req.users.findById(result1.upUser, function(err, result2){
					result1.upUser = result2
					res.locals.isFans = 'false'
					if (result2.Fans=='' || result2.Fans==null) {
						res.render('video',{
							'title':'vidoe',
							'user':req.infos,
							'video':result1,
							'av':req.params.id
						})
					} else {
						for (var i = 0; i <= result2.Fans.length; i++) {
							if ((result2.Fans[i]+'') == (req.infos._id+'')){
								res.locals.isFans = "true"
								break;
							} else {
								res.locals.isFans = 'false'
							}
						}
						res.render('video',{
							'title':'vidoe',
							'user':req.infos,
							'video':result1,
							'av':req.params.id
						})
					}
				})
			}
		})
	}catch(e){
		res.status(404).render('404',{'err':e})
	}
});

router.get('/:tag', function(req, res, next){
	var tag = req.params.tag;
	VandB.findTag(req, tag, function(d){
		res.render('./video/tagvideo',{'title':tag,'user':req.infos,'video':d.dt})
	})
})

router.put('/av/send/:id', function(req, res, next){
	console.log(req.body)
	res.send('s')
})

router.get('/av/comments/:id', function(req, res, next){
	try{
		Comments.findComments(req, 'video', req.params.id, function(j){
			if (j.status == 200) {
				res.json(j.data)
			} else {
				res.status(200).send('not find')
			}
		})
	}catch(e){
		res.status(404).render('404',{'err':e})
	}
})

router.post('/av/comments/:id', function(req, res, next){
	if (req.infos===null) {
		res.status(400).send('Bad Request');
	} else {
		Comments.insertComments(req, 'video', req.params.id, function(j){
			if (j.status == 200) {
				res.status(200).send(j.data)
			} else {
				res.status(400).send(j.data)
			}
		})
	}
})

router.post('/av/danmu/:id', function(req, res, next){
	try{
		if (!req.infos) {
			res.status(400).send('Bad Request');
		} else {
			req.danmu.find({'av':req.params.id}).toArray(function(err, items){
				if (err) {
					res.status(400).send('Bad Request');
				} else {
					if (items==null || items.length == 0) {
						req.danmu.insert({
							'av':req.params.id,
							'comments':[{
								'uid':req.body.uid,
								'danmu':req.body.danmuText,
								'color':req.body.color,
								'vtime':req.body.vtime,
								'stime':new Date().getTime()
							}]
						},function(err, result){
							res.send('ok')
						})
					} else {
						req.danmu.update({'av':req.params.id},
										 {$push:{
										 	'comments':{
												'uid':req.body.uid,
												'danmu':req.body.danmuText,
												'color':req.body.color,
												'vtime':req.body.vtime,
												'stime':new Date().getTime()
											}
										 }},function(err, result){
										 	res.send('ok')
										 })
					}
				}
			})
		}
	}catch(e){

	}
	
})

router.get('/av/keep/:id', function(req, res, next){
	try{
		if (!req.infos.name) {
			res.status(404).render('404',{'err':'please login'})
		}else{
			keep.insertKeep(req, req.infos.name, 'video', req.params.id, function(d){
				res.send(d)
			})
		}
	}catch(e){

	}
})

module.exports = router;
